<template>
  <div class="order">
    <el-row :gutter="10">
      <el-col :span="24">
        <el-card class="box-card mt-15">
          <div class="home-form">
            <el-form ref="ruleForm" label-width="80px" @submit.native.prevent>
              <el-row :gutter="10">
                <el-col :span="6">
                  <el-form-item label="支付时间" prop="payTime">
                    <el-date-picker
                      style="width: 100%"
                      size="mini"
                      value-format="yyyy-MM-dd HH:mm:ss"
                      v-model="ruleForm.payTime"
                      type="datetimerange"
                      range-separator="至"
                      start-placeholder="开始日期"
                      end-placeholder="结束日期">
                    </el-date-picker>
                  </el-form-item>
                </el-col>
                <el-col :span="4">
                  <el-form-item label="关联教练" prop="teachCoachUserId">
                    <el-select v-model="ruleForm.teachCoachUserId" placeholder="请选择" style="width:100%" clearable size="mini">
                      <el-option
                        v-for="item in coachArr"
                        :key="item.id"
                        :label="item.userName"
                        :value="item.id">
                      </el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="4">
                  <el-form-item label="会员" prop="key">
                    <el-input  placeholder="请输入会员教练员手机号" size="mini" v-model.trim="ruleForm.key" clearable></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="4">
                  <el-button type="primary" icon="el-icon-search" size="mini" @click="handleSearch" class="search-btn">搜索</el-button>
                </el-col>
              </el-row>
            </el-form>
          </div>
        </el-card>
        <el-card class="box-card">
          <!-- 头部 -->
          <div slot="header" class="clearfix fix-lh">
            <span>订单管理</span>
            <div class="btn-list">
              <!-- <el-button size="mini" v-hasPermission="permission.add" >批量导出</el-button> -->
            </div>
          </div>
          <div class="home-body">
            <cvue-table
              :option="tableOption"
              :data="tableData"
              ref="tbl1"
              :isShowAdd="false"
              :page="page"
              :tablePage="tablePage"
              :pageSize="page.pageSize"
              :loading="tableLoading"
              @current-change="handleCurrentChange"
              @size-change="handleSizeChange"
              :isShowPage="true">
              <template slot-scope="scope" slot="status">
                <span>{{ setStatus(scope.row.status) }}</span>
              </template>
              <template slot-scope="scope" slot="payMoney">
                <span>{{ (scope.row.payMoney/100).toFixed(2) }}</span>
              </template>
            </cvue-table>
          </div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import {mapGetters} from 'vuex'
import cvueTable from '@/components/cvue-table'
export default {
  name: 'order-manage',
  components: {
    cvueTable
  },
  data () {
    return {
      tablePage: 1,
      tableLoading: false,
      page: {
        total: 1, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 10, // 每页显示多少条
        pageSizes: [10, 50, 100]
      },
      tableOption: {
        stripe: true,
        width: '100%',
        border: true,
        index: false,
        selected: false,
        cloumn: [
          { prop: 'index', label: '', align: 'center', width: 58 },
          { prop: 'id', label: '订单号', align: 'center', sortable: true },
          { prop: 'payTime', label: '支付时间', align: 'center', sortable: true },
          { prop: 'userName', label: '会员姓名', align: 'center', sortable: true },
          { prop: 'phone', label: '会员电话', align: 'center', sortable: true },
          { prop: 'courseName', label: '购买内容', align: 'center', sortable: true },
          { prop: 'payMoney', label: '支付金额', align: 'center', sortable: true, slotStatus: true },
          // { prop: 'refund_type', label: '成交教练', align: 'center', sortable: true },
          { prop: 'teachCoachUserName', label: '关联教练', align: 'center', sortable: true },
          { prop: 'payType', label: '支付方式', align: 'center', sortable: true },
          { prop: 'updateUserName', label: '操作人', align: 'center', sortable: true }
        ]
      },
      tableData: [
        // { name: 'ddd', code: 'sss', order: 1, status: 1 }
      ],
      dialogOrderEdit: false,
      actionType: 'add',
      rowData: {},
      statusArr: [
        {name: '已取消', value: 0},
        {name: '未付款', value: 10},
        {name: '待发货', value: 20},
        {name: '待收货', value: 30},
        {name: '待评价', value: 40},
        {name: '交易完成', value: 50},
        {name: '交易关闭', value: 51}
      ],
      ruleForm: {
        payTime: [],
        teachCoachUserId: '',
        key: ''
      },
      selectForm: {
        payTime: [],
        teachCoachUserId: '',
        key: ''
      },
      coachArr: []
    }
  },
  computed: {
    isDelete () {
      return false
    },
    ...mapGetters(['token', 'userInfo'])
  },
  created () {
    this.handleList(this.selectForm.payTime, this.selectForm.teachCoachUserId, this.selectForm.key)
    this.getCoachList()
  },
  methods: {
    getCoachList () {
      var params = {
        current: 1,
        size: 1000,
        key: ''
      }
      this.$store.dispatch('manage/CoachEnabledGet', params).then(res => {
        // console.log(res)
        if (res.code == '200') {
          var data = res.data
          this.coachArr = data
        }
      }).catch(err => {
        console.log(err)
      })
    },
    setStatus (status) {
      if (status == 0) {
        return '已取消'
      } else if (status == 10) {
        return '未付款'
      } else if (status == 20) {
        return '待发货'
      } else if (status == 30) {
        return '待收货'
      } else if (status == 40) {
        return '待评价'
      } else if (status == 50) {
        return '交易完成'
      } else if (status == 51) {
        return '交易关闭'
      } else {
        return '其他'
      }
    },
    setRefundType (type) {
      if (type == 0) {
        return '正常'
      } else if (type == 1) {
        return '申请退款'
      } else if (type == 2) {
        return '已退款'
      }
    },
    // 新增
    handleAdd () {
      this.actionType = 'add'
      this.dialogOrderEdit = true
    },
    // 改变pagesize
    handleSizeChange (val) {
      this.page.pageSize = val
      this.handleList(this.selectForm.payTime, this.selectForm.teachCoachUserId, this.selectForm.key)
    },
    // 改变页码
    handleCurrentChange (val) {
      this.tablePage = val
      this.handleList(this.selectForm.payTime, this.selectForm.teachCoachUserId, this.selectForm.key)
    },
    // 列表接口
    handleList (payTime, teachCoachUserId, key) {
      this.tableLoading = true
      var params = {
        'size': this.page.pageSize,
        'current': this.tablePage,
        'payTimeStart': payTime[0] ? payTime[0] : '',
        'payTimeEnd': payTime[1] ? payTime[1] : '',
        'teachCoachUserId': teachCoachUserId,
        'key': key,
        'orders[0].order': 'desc',
        'orders[0].sort': 'createTime'
      }
      this.$store.dispatch('manage/OrderGet', params).then(res => {
        console.log(res)
        if (res.code == 200) {
          var data = res.data.records
          this.tableData = data
          this.page.total = parseInt(res.data.total)
          this.page.currentPage = this.tablePage
        }
        this.tableLoading = false
      }).catch(err => {
        console.log(err)
        this.tableLoading = false
      })
    },
    // 关闭弹窗
    closeDialog (params) {
      if (params == 'edit') {
        this.dialogOrderEdit = false
      }
    },
    // 确认
    confirmDialog (params) {
      if (params == 'edit') {
        this.dialogOrderEdit = false
        this.tablePage = 1
        this.handleList(this.selectForm.payTime, this.selectForm.teachCoachUserId, this.selectForm.key)
      }
    },
    handleSearch () {
      this.selectForm.payTime = this.ruleForm.payTime ? this.ruleForm.payTime : []
      this.selectForm.teachCoachUserId = this.ruleForm.teachCoachUserId
      this.selectForm.key = this.ruleForm.key
      this.tablePage = 1
      this.handleList(this.selectForm.payTime, this.selectForm.teachCoachUserId, this.selectForm.key)
    }
  }
}
</script>

<style lang="scss">
.order {
  padding: 0px !important;
  height: auto !important;
  .btn-list {
    float: right;
    margin-right: 10px;
    font-size: 12px;
    color: #409EFF;
    cursor: pointer;
  }
  .el-card__header {
    border-bottom: none;
  }
  .el-card__header,
  .el-card__body {
    padding: 15px;
  }
  .fix-lh {
    line-height: 32px;
  }
  .el-input.is-disabled .el-input__inner {
    color: #606266;
  }
  .left{
    min-height:500px;
    background:#fff;
  }
  .left-tree{
    width: 100%;
    min-height:600px;
    overflow: auto;
  }
  .left-top{
    width: 100%;
    height: 40px;
    text-align: center;
    line-height: 40px;
    font-size: 16px;
    color: #333;
    background: #e2e2e2;
    font-size: 15px;
  }
}
</style>
